package com.brightworks.scripthelper.analyzers
{
    import com.brightworks.scripthelper.constants.LanguageCodeConstants;
    import com.brightworks.scripthelper.constants.LineTypeConstants;
    import com.brightworks.scripthelper.constants.MiscConstants;
    import com.brightworks.util.Log;
    import com.brightworks.util.Utils_String;

    public class LineTypeAnalyzer_Comment extends LineTypeAnalyzer
    {
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        //
        //          Getters & Setters
        //
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

        public static function get lineTypeDescription():String
        {
            return LineTypeConstants.LINE_TYPE_DESCRIPTION__COMMENT;
        }

        public static function get lineTypeId():String
        {
            return LineTypeConstants.LINE_TYPE_ID__COMMENT;
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        //
        //          Public Methods
        //
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

        public function LineTypeAnalyzer_Comment()
        {
            super();
        }

        public function extractVoiceScriptNoteFromComment():String
        {
            if (!isVoiceScriptNoteComment())
                Log.fatal("LineTypeAnalyzer_Comment.extractVoiceScriptNoteFromComment(): Comment isn't a voice script comment");
            var firstColonIndex:int = lineText.indexOf(":");
            var result:String = lineText.substring(firstColonIndex + 1);
            result = Utils_String.removeWhiteSpaceIncludingLineReturnsFromBeginningAndEndOfString(result);
            return result;
        }

        override public function getProblemReports():Array
        {
            var problemList:Array = [];
            if ((isIgnoreProblemsComment()) && (getIgnoreProblemsExplanation().length < 2))
            {
                problemList.push('This line says "' + MiscConstants.IGNORE_PROBLEMS_TAG + '" but it does not include an explanation.');
            }
            if (isVoiceScriptNoteComment())
            {
                if (getLanguageCodeForVoiceScriptNote().length != 3)
                {
                    problemList.push('This line says "' + MiscConstants.VOICE_SCRIPT_NOTE_TAG + '" but it does not include a language code followed by a colon. Supported language codes are: ' + LanguageCodeConstants.getLanguageCodeListString());
                }
                else
                {
                    if (extractVoiceScriptNoteFromComment().length < 2)
                    {
                        problemList.push('This line says "' + MiscConstants.VOICE_SCRIPT_NOTE_TAG + '" but it does not seem to contain a note.');
                    }
                }
            }
            return constructFullProblemStrings(problemList);
        }

        public function isIgnoreProblemsComment():Boolean
        {
            return (lineText.indexOf(MiscConstants.IGNORE_PROBLEMS_TAG) != -1);
        }

        public static function isLineThisType(lineString:String, lineStringList:Array=null, lineNum:int=-1):Boolean
        {   // "Comment" lines start with ">>"
            if (lineString.indexOf(">>") == 0)
                return true;
            return false;
        }

        public function isVoiceScriptNoteCommentForLanguage(languageCode:String):Boolean
        {
            return (isVoiceScriptNoteComment() && (languageCode == getLanguageCodeForVoiceScriptNote()))
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        //
        //          Private Methods
        //
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

        private function getIgnoreProblemsExplanation():String
        {
            var ignoreProblemsTagIndex:int = lineText.indexOf(MiscConstants.IGNORE_PROBLEMS_TAG);
            if (ignoreProblemsTagIndex == -1)
                Log.fatal("LineTypeAnalyzer_Comment.getIgnoreProblemsExplanation(): line text doesn't include IGNORE_PROBLEMS_TAG");
            var explanationStartIndex:int = ignoreProblemsTagIndex + MiscConstants.IGNORE_PROBLEMS_TAG.length;
            var explanation:String = lineText.substring(explanationStartIndex, lineText.length);
            explanation = Utils_String.removeWhiteSpaceIncludingLineReturnsFromBeginningAndEndOfString(explanation);
            return explanation;
        }

        private function getLanguageCodeForVoiceScriptNote():String
        {
            if (!isVoiceScriptNoteComment())
                return "";
            var firstColonIndex:int = lineText.indexOf(":");
            if (firstColonIndex < 20)
                return "";
            var languageCode:String = lineText.substring(firstColonIndex - 3, firstColonIndex);
            if (!LanguageCodeConstants.isStringLanguageCode(languageCode))
                return "";
            return languageCode;
        }

        private function isVoiceScriptNoteComment():Boolean
        {
            return (lineText.indexOf(MiscConstants.VOICE_SCRIPT_NOTE_TAG) > -1);
        }

    }
}
















